<template>
  <div>
    <div class="bg">
      <div class="containers">
        <div class="left">
        </div>
        <div class="right">
          <p class="title">职统</p>
          <p class="content">企业高效办公平台</p>
          <el-form :model="login" status-icon :rules="rules" ref="login" label-width="55px" class="login-form">
            <el-form-item label="用户名" prop="username">
              <el-input placeholder="请输入用户名" type="text" v-model="login.username" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input placeholder="请输入密码" type="password" v-model="login.password" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item class="buttons">
              <el-button type="primary" @click="submit('login')">登录</el-button>
              <el-button @click="reset('login')">注册</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { initRequestParams } from '@/request'

export default {
  name:'loginPage',
  data() {
    let validateUserName = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入用户名'));
      } else {
        callback();
      }
    };
    let validatePassword = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        // if (this.login.password !== '') {
        //   this.$refs.login.validateField('checkPassword');
        // }
        callback();
      }
    };
    return {
      login: {
        username: '',
        password: '',
      },
      rules: {
        username: [
          { validator: validateUserName, trigger: 'blur' }
        ],
        password: [
          { validator: validatePassword, trigger: 'blur' }
        ]
      }
    };
  },
  async mounted() {
    
  },
  methods: {
    async submit(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          let { code, data, msg } = await this.$post('/dologin', {
            data: {
              username: this.login.username,
              password: this.login.password
            }
          })
          console.log(data,msg)
          if(code === 200) { // 登录成功
            localStorage.setItem('username',data.username)
            localStorage.setItem('role',data.role)
            localStorage.setItem('userinfo', JSON.stringify(data))
            this.$store.commit('user/setUser', data)
            initRequestParams(this.$store.getters['user/token'])
            this.$router.push('/main/nav')
          } else {
            this.$message.error(msg)
          }
          
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    reset(formName) {
      console.log(formName)
      // this.$refs[formName].resetFields();
      this.$router.push('/register')
    }
  }
}
</script>

<style scoped lang="scss">
.bg {
  width: 100vw;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: url('~@/assets/mainicon/bg.png');
  background-size: 100vw 100vh;

  .containers {
    width: 600px;
    min-height: 400px;
    border-radius: 20px;
    justify-content: center;
    align-items: center;
    background:white;
    display: flex;

    .left {
      width: 200px;
      height: 200px;
      background: url('~@/assets/mainicon/bgbg.png');
      background-size: 200px 200px;
    }

    .right {
      width: 300px;

      .title {
        // text-align: center;
        font-size: 20px;
      }

      .content{
        // text-align: center;
        margin-top: 20px;
        margin-bottom: 20px;
        font-size: 12px;
        color: #b4b4b4;
      }

      .login-form {
        width: 300px;

        .buttons {
          display: flex;
        }
      }
    }
  }
}



</style>